<?php 
namespace cubox\webpack;

use cubox\Webpack;
use cubox\provider\webpack\Html;
use cubox\provider\template\Asset;
class Vue  extends Html{

    public function pack(){
        $content = $this->getContent();
        

        $asset = new Asset($this->name,$content,$this->file,$this->webpackUrl);
        $content = $asset->make();

        $this->compileStyle($content);
        $template = $this->parseTemplateTag($content);
        $script   = $this->compileScript($content);
        
        $funName  = $this->getFunctionName($this->file);

        $tr = "\n";
        $js =$tr."function ".$funName."(){";
        $js.=$tr. 'let  ___con = ()=>{ ';
        $js.=$tr. $script;
        $js.=$tr. '}';
        $js.=$tr. 'let ___c = ___con();';
        if($template!='')   $js.=$tr. "___c['template']= `".$template."`";
        $js.=$tr. 'return ___c; ' ;
        $js.=$tr. '};';

        Webpack::addJs($this->name,$js);
        if (isset($this->param['name']) && !$this->import){

            if (strpos($this->param['name'],'[')===false){
                $script = "let ".$this->param['name']." = ".$funName.'();';    
            }else{
                $script = "".$this->param['name']." = ".$funName.'();';
            }
            
            Webpack::addScript($this->name,$script);
        }
    }

    
    /**
     * 编译样式
     *
     * @param string $content
     * @return void
     */
    protected function compileStyle(string $content=""){
        $styleList = $this->parseHtml($content,'style');

        $cssCode = '';
        if (count($styleList)>0){
            foreach($styleList as $val){
                $cssCode .= $val['content'];
            }
        }
        $cssCode = $this->cssImport($cssCode);
        //$cssCode = $this->compileImageUrl($cssCode);

        webpack::addCss($this->name,$cssCode);

    }

    /**
     * 编译脚本
     *
     * @param string $content
     * @return string
     */
    protected function compileScript(string $content=""){
        $scriptList = $this->parseHtml($content,'script');
        
        $scriptCode = "";

        if (count($scriptList)>0){
            foreach($scriptList as $val){
                $scriptCode .= $val['content'];
            }

            $scriptCode = $this->jsImport($scriptCode);
            $scriptCode = $this->export($scriptCode);
        }else{
            $scriptCode = "return {};";
        }



        return $scriptCode;
    }


    /**
     * 解析常规标签 (template除外)
     *
     * @param string $html
     * @param string $tag
     * @return array
     */
    protected function parseHtml(string $html,string $tag):array{ 
        $re = '/<'.$tag.'([^<|^>]*)>([\s\S]*?)<\/'.$tag.'>/i';
 
        preg_match_all ($re, $html, $match);
        $html  = '';
        $data  = array();
         
        foreach($match[0] as $k=>$v){
            $data [$k] = array(
                'param'=>$match[1][$k],
                'input'=>$match[0][$k],
                'content'=>$match[2][$k]
            );
        }

        return  $data ;
    }

    /**
     * 解析Template标签
     *
     * @param string $html
     * @return string
     */
    protected function parseTemplateTag(string $html=""):string{

        $tag = 'template';

        $re = '/<'.$tag.'([^<|^>]*)>([\s\S]*?)<\/'.$tag.'>/U';
        
        preg_match_all ($re, $html, $match);
         
        if (isset($match[2]) && !empty($match[2])){
            
            return $match[2][0];
        }else{
            return '';
        }

    }

    public static function variable(){
        return true;
    }

} 